package com.wiken.storeservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.PageUtill;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.VO.CommodityCategoriesVO;
import com.wiken.storeservice.entity.Commodity;
import com.wiken.storeservice.entity.Store;
import com.wiken.storeservice.entity.StoreCategories;
import com.wiken.storeservice.mapper.CommodityMapper;
import com.wiken.storeservice.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {

    @Autowired
    StoreServiceImpl storeService;


    //检查商品是否是属于店主的商品
    public boolean isMyStoreOfCommodity(Commodity commodity, String token){
        Claims claims = TokenUtil.parseToken(token);

        if(claims == null) throw new DefaultException(Code.ERROR_AUTHORIZATION_EXPIRATION, "账号token过期");

        String userId = claims.get("userId", String.class);

        if(userId == null) throw new DefaultException(Code.ERROR_AUTHORIZATION, "请检查账号是否正常");

        Store store = baseMapper.isMyStoreOfCommodity(commodity, userId);

        return store != null;
    }

    @Override
    public boolean addCommodity(Commodity commodity, String token) {

        if(!isMyStoreOfCommodity(commodity, token)) throw new DefaultException(Code.ERROR_AUTHORIZATION, "添加失败");

        if(commodity.getStatus() == null) commodity.setStatus("1");

        int success = baseMapper.insert(commodity);

        return success > 0;
    }

    @Override
    public boolean updateComodity(Commodity commodity, String token) {
        if(!isMyStoreOfCommodity(commodity, token)) throw new DefaultException(Code.ERROR_AUTHORIZATION, "更新失败");

        int success = baseMapper.updateById(commodity);
        return success > 0;
    }

    @Override
    public Page getCommodityListByCategoryId(StoreCategories storeCategories, PageUtill pageUtill) {
        Page page = new Page();
        page.setCurrent(pageUtill.getCurrent());
        page.setSize(pageUtill.getSize());

        Page commodityList = baseMapper.selectPage(page, new QueryWrapper<Commodity>().eq("categoriesId", storeCategories.getId()));

        return commodityList;
    }

    @Override
    public Page getCommodityListByUserId(Page page, String token) {
        String userId = TokenUtil.getUserIdByToken(token);

        Page<CommodityCategoriesVO> commodityList = baseMapper.getCommodityListByUserId(page, userId);
        return commodityList;
    }

    @Override
    public boolean delCommodity(Commodity commodity, String token) {
        if(!isMyStoreOfCommodity(commodity, token)) throw new DefaultException(Code.ERROR_AUTHORIZATION, "删除失败");

        int success = baseMapper.deleteById(commodity);

        return success > 0;
    }
}
